package org.example.page;

import lombok.extern.log4j.Log4j2;
import org.example.param.Parameter;

import javax.imageio.ImageIO;
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

@Log4j2
public class SignaturePanel extends JPanel implements ActionListener {

    private JPanel cardPanel;
    private CardLayout cardLayout;
    private Point startPoint;
    private Point endPoint;
    private BufferedImage image;
    private String savePath = "D:\\test";

    private List<List<LocalPoint>> part = new ArrayList<>();
    private List<LocalPoint> littlePart = new ArrayList<>();

    public SignaturePanel(JPanel cardPanel, CardLayout cardLayout) {
        this.cardPanel = cardPanel;
        this.cardLayout = cardLayout;

        btnInit();

        listenerInit();

    }

    private void listenerInit() {

        image = new BufferedImage(Parameter.APPLICATION_WIDTH,Parameter.APPLICATION_HEIGHT,BufferedImage.TYPE_INT_ARGB);

        this.addMouseListener(new MouseAdapter() {
            @Override
            public void mousePressed(MouseEvent e) {
                startPoint = e.getPoint();
            }

            @Override
            public void mouseReleased(MouseEvent e) {
                endPoint = e.getPoint();
                part.add(littlePart);
                littlePart = new ArrayList<>();
                drawImage();
            }
        });

        this.addMouseMotionListener(new MouseAdapter() {
            @Override
            public void mouseDragged(MouseEvent e) {
                endPoint = e.getPoint();
                littlePart.add(new LocalPoint(startPoint,endPoint));
                drawImage();
                startPoint = endPoint;
            }
        });


    }

    private void drawImage() {
        Graphics2D graphics = image.createGraphics();
        graphics.setStroke(new BasicStroke(3));
        //清空画面
        graphics.setColor(Color.white);
        graphics.fillRect(0,0,Parameter.APPLICATION_WIDTH,Parameter.APPLICATION_HEIGHT);

        //绘画以前的笔画
        graphics.setColor(Color.BLACK);
        for (List<LocalPoint> localPoints : part) {
            for (LocalPoint localPoint : localPoints) {
                graphics.drawLine(localPoint.startPoint.x,localPoint.startPoint.y,localPoint.endPoint.x,localPoint.endPoint.y);
            }

        }

        //绘画现在正在画的笔画
        for (LocalPoint localPoint : littlePart) {
            graphics.drawLine(localPoint.startPoint.x,localPoint.startPoint.y,localPoint.endPoint.x,localPoint.endPoint.y);

        }

            //此时是导致撤销不干净的原因
//        graphics.drawLine(startPoint.x,startPoint.y,endPoint.x,endPoint.y);

        repaint();



//        graphics.setColor(Color.BLACK);
//        graphics.drawLine(startPoint.x,startPoint.y,endPoint.x,endPoint.y);
//        repaint();
//        startPoint = endPoint;
//

    }

    @Override
    public void paint(Graphics g) {
        super.paint(g);
        g.drawImage(image,0,0,this);
    }

    private void btnInit() {
        JButton returnBtn = new JButton(Parameter.RETURN_BTN);
        JButton ensureBtn = new JButton(Parameter.ENSURE_BTN);
        JButton undoBtn = new JButton(Parameter.UNDO_BTN);

        returnBtn.addActionListener(this);
        ensureBtn.addActionListener(this);
        undoBtn.addActionListener(this);


        this.add(returnBtn);
        this.add(ensureBtn);
        this.add(undoBtn);

    }


    @Override
    public void actionPerformed(ActionEvent e) {
        if (e.getSource() instanceof JButton){

            JButton btn = (JButton) e.getSource();

            switch (btn.getText()){

                case Parameter.RETURN_BTN :
                    cardLayout.show(cardPanel,"mainPanel");
                    break;
                case Parameter.ENSURE_BTN:
                    //签名写好，保存为图片
                    log.info("开始保存签名");
                    saveSignature(savePath);
                    log.info("保存签名完毕");
                    //清空
                    image = new BufferedImage(Parameter.APPLICATION_WIDTH,Parameter.APPLICATION_HEIGHT,BufferedImage.TYPE_INT_ARGB);
                    cardLayout.show(cardPanel,"mainPanel");

                    break;

                case Parameter.UNDO_BTN:
                    undo();
                    break;
            }




        }
    }

    private void undo() {
        if (!part.isEmpty()){
            part.remove(part.size() - 1);
            drawImage();
        }
    }

    private void saveSignature(String path) {
        File file = new File(path);
        if (!file.exists()){
            file.mkdirs();
        }
        try {
            ImageIO.write(image,"png",new File(path + "\\my.png"));
            clearDrawImage();
        } catch (IOException e) {
            log.error("保存图片失败");
            throw new RuntimeException(e);
        }


    }

    private void clearDrawImage() {
        part = new ArrayList<>();
        littlePart = new ArrayList<>();
    }

    private class LocalPoint {

        public Point startPoint;
        public Point endPoint;
        public LocalPoint(Point startPoint, Point endPoint) {

            this.startPoint = startPoint;
            this.endPoint = endPoint;
        }
    }
}
